package com.zzz.gp.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @description:
 * @author: ZZZ
 * @time: 2021/8/8 15:54
 */
@Configuration
public class ThreadPoolTaskExecutorConfig {

    @Bean("myAsync")
    public ThreadPoolTaskExecutor myAsync() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //核心池大小
        executor.setCorePoolSize(5);
        //队列程度
        executor.setQueueCapacity(2000);
        //最大线程数
        executor.setMaxPoolSize(100);
        //线程空闲时间
        executor.setKeepAliveSeconds(1000);
        //线程前缀名称
        executor.setThreadNamePrefix("gp-thread-");
        //配置拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        //初始化
        executor.initialize();
        return executor;
    }
}
